package com.axdoc.baseinfo.dao.impl;

import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Component;

import com.axdoc.baseinfo.dao.ICityMongoDao;
import com.axdoc.baseinfo.entity.City;
import com.axdoc.common.core.mongo.AbstractMongoDao;
import com.axdoc.common.core.pagination.PageInfo;
import com.axdoc.common.core.pagination.PageResults;
import com.mongodb.BasicDBObject;
@Component("cityMongoDao")
public class CityMongoDaoImpl extends AbstractMongoDao<City> implements ICityMongoDao{

	public City getCity(ObjectId cityId) {
		return super.get(City.MONGO_COLLECT_NAME, cityId);
	}

	public PageResults<City> findCityPage(City cityCon, PageInfo pageInfo) {
		BasicDBObject query = new BasicDBObject();
		if(StringUtils.isNotEmpty(cityCon.getName())){
			Pattern namePattern = Pattern.compile("^.*" + cityCon.getName() + ".*$", Pattern.CASE_INSENSITIVE);
			query.put("name", namePattern);
		}
		if(StringUtils.isNotEmpty(cityCon.getRegion())){
			query.put("region", cityCon.getRegion());
		}
		if(cityCon.getLevel() != null){
			query.put("level", cityCon.getLevel());
		}
		if(cityCon.getIs_on() != null){
			query.put("is_on", cityCon.getIs_on());
		}
		BasicDBObject orderBy = new BasicDBObject();
		orderBy.put("ct", -1);
		List<City> result = super.find(City.MONGO_COLLECT_NAME, query, orderBy, pageInfo);
		return new PageResults<City>(pageInfo, result);
	}

	public City getCityByName(String name) {
		return super.getByPropery(City.MONGO_COLLECT_NAME, "name", name);
	}
	
	public List<City> findCityListByName(String name) {
		BasicDBObject query = new BasicDBObject();
		Pattern namePattern = Pattern.compile("^.*" + name + ".*$", Pattern.CASE_INSENSITIVE);
		query.put("name", namePattern);
		return super.find(City.MONGO_COLLECT_NAME, query);
	}
}
